﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Networking;

namespace PoolGame2.Menus
{
    public class LobbyMenu : Menu
    {
        public class ReadyItem : MenuItem
        {
            public bool State = false;
            public ReadyItem(string text, PoolGame game) : base( text, game )
            {
            }
        };
 
        ReadyItem readyButton;
        public LobbyMenu(PoolGame game)
            : base(game)
        {
            backgroundTexture = game.textureDatabase.GetTexture("background_lobby");

            int menuspacing = 70;
            int pos = 60;

            readyButton = new ReadyItem("Set Ready", game);
            readyButton.Position = new Vector2(40, pos);
            readyButton.onSelected = new EventHandler(readySelected);
            AddMenuItem(readyButton);
            pos += menuspacing;

            MenuItem item = new MenuItem("Exit", game);
            item.Position = new Vector2(40, pos);
            item.onSelected = new EventHandler(exitSelected);
            AddMenuItem(item);
        }

        public void exitSelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            pgame.EndGame();
            pgame.menuManager.SwitchMenu(MenuManager.eMenuTypes.MENU_MAIN);
        }

        public override void  OnOpen()
        {
            PoolGame pgame = (PoolGame)Game;
            foreach (LocalNetworkGamer gamer in pgame.networkManager.netSession.LocalGamers)
            {
                readyButton.State = false;
                gamer.IsReady = readyButton.State;
                readyButton.Text = "Set Ready";
            }
 	        
            base.OnOpen();
        }
        public void readySelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            foreach (LocalNetworkGamer gamer in pgame.networkManager.netSession.LocalGamers)
            {
                readyButton.State = !readyButton.State;
                gamer.IsReady = readyButton.State;

                if (readyButton.State)
                    readyButton.Text = "Set Not Ready";
                else
                    readyButton.Text = "Set Ready";
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            PoolGame pgame = (PoolGame)Game;
            NetworkManager manager = pgame.networkManager;
            if (manager == null)
                return;

            SpriteBatch spriteBatch = pgame.spriteBatch;
            SpriteFont font = pgame.fontDatabase.loadFont("Courier New");
            if (manager.connectedPlayers.Count > 0)
            {
                NetworkGamer gamer = manager.connectedPlayers[0].GetNetworkGamer();
                if (gamer.IsLocal)
                {
                    GamerProfile profile = gamer.GetProfile();
                    Vector2 position = new Vector2(550, 350);
                    Rectangle fullscreen = new Rectangle((int)(position.X), (int)position.Y, (int)150, (int)150);

                    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                    spriteBatch.Draw(profile.GamerPicture, fullscreen, Color.White);
                    spriteBatch.DrawString(font, gamer.Gamertag, new Vector2(position.X, position.Y + 175), Color.Black);
                    if( gamer.IsReady )
                        spriteBatch.DrawString(font, "Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    else
                        spriteBatch.DrawString(font, "Not Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    spriteBatch.End();
                }
                else
                {
                    Vector2 position = new Vector2(950, 350);

                    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                    spriteBatch.DrawString(font, gamer.Gamertag, new Vector2(position.X, position.Y + 175), Color.Black);
                    if (gamer.IsReady)
                        spriteBatch.DrawString(font, "Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    else
                        spriteBatch.DrawString(font, "Not Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    spriteBatch.End();
                }
            }
            
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                spriteBatch.DrawString(pgame.fontDatabase.loadFont("Courier New"), "Versus", new Vector2(840, 450), Color.Black);
            spriteBatch.End();

            if( manager.connectedPlayers.Count > 1 )
            {
                NetworkGamer gamer = manager.connectedPlayers[1].GetNetworkGamer();
                if (gamer.IsLocal)
                {
                    GamerProfile profile = gamer.GetProfile();
                    Vector2 position = new Vector2(550, 350);
                    Rectangle fullscreen = new Rectangle((int)(position.X), (int)position.Y, (int)150, (int)150);

                    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                    spriteBatch.Draw(profile.GamerPicture, fullscreen, Color.White);
                    spriteBatch.DrawString(font, gamer.Gamertag, new Vector2(position.X, position.Y + 175), Color.Black);
                    if (gamer.IsReady)
                        spriteBatch.DrawString(font, "Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    else
                        spriteBatch.DrawString(font, "Not Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    spriteBatch.End();
                }
                else
                {
                    Vector2 position = new Vector2(950,350);
                    
                    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                    spriteBatch.DrawString(font, gamer.Gamertag, new Vector2(position.X, position.Y + 175), Color.Black);
                    if (gamer.IsReady)
                        spriteBatch.DrawString(font, "Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    else
                        spriteBatch.DrawString(font, "Not Ready", new Vector2(position.X, position.Y + 175 + font.LineSpacing), Color.Black);
                    spriteBatch.End();
                }
            }
        }
    }
}
